import {
	payPay
} from '@/api/user/pay.js'
import app from "@/main.js"
export const payAction = (payType, orderNumber) => {
	// 返回promise
	return new Promise((resolve, reject) => {
		console.log(payType, orderNumber)
		var pages = getCurrentPages();
		var page = pages[pages.length - 1];
		const jumpFn = t => {
			let { route } = page
			let toPath = `/pages/user/order/pay/result/result?type=${ t }&orderNumber=${ orderNumber }`
			let orderDetailPath = 'pages/user/order/detail/detail'
			let payDetailPath = 'pages/user/order/pay/result/result'
			if (route && (route.indexOf(orderDetailPath) > -1 || route.indexOf(payDetailPath) > -1)) {
				uni.redirectTo({
					url: toPath
				})
			} else {
				app.jumpUrl(toPath)
			}
		}
		if (payType !== 3) {
			payPay({
				payType,
				orderNumber
			}).then((data) => {
				app.$store.dispatch('initUserData')
				app.$api.msg('支付成功')
				let timer = setTimeout(() => {
					resolve(data)
					jumpFn('success')
				}, 1000)
			}).catch((e) => {
				reject(e)
				jumpFn('failure')
			})
		} else {
			uni.navigateTo({
				url: `/pages/other/paypal/paypal`,
				events: {
					change: e => {
						if (e) {
							resolve(e)
							jumpFn('success')
						} else {
							reject(e)
							jumpFn('failure')
						}
					}
				},
				success: res => {
					res.eventChannel.emit('paypal-page', orderNumber)
				}
			})
		}
		// payPay({
		// 	payType,
		// 	orderNumber
		// }).then((data) => {
		// 	app.$store.dispatch('initUserData')
		// 	if (payType !== 3) {
		// 		app.$api.msg('支付成功')
		// 		let timer = setTimeout(() => {
		// 			resolve(data)
		// 		}, 1000)
		// 	} else {
		// 		uni.navigateTo({
		// 			url: `/pages/other/paypal/paypal`,
		// 			events: {
		// 				change: e => {
		// 					if (e) {
		// 						resolve(data)
		// 					} else {
		// 						reject(e)
		// 					}
		// 				}
		// 			},
		// 			success: res => {
		// 				console.log('data => ', data)
		// 				res.eventChannel.emit('paypal-page', data)
		// 			}
		// 		})
				// 获取订单ID
				// let orderInfo = {
				// 	// clientId: "AfkJlX5tL0XJ2MAxTnRMjdafWqtfo653p5vElDgTf_N9OdTEzv1O82c2CgwgLM5azUseWhXCArTG",
				// 	clientId: "AStueIBCNsQhPXWLYsSB--12C8DBa-5dAK_EhYbsXgsjvHDPxnx46yrKDrL-mNmV3AZcWRmh7jDfv_dw", //开发者中心clientId
				// 	orderId: data, //订单id
				// 	userAction: "continue", //按钮样式 paynow/continue
				// 	currency: "USD", //币种
				// 	environment: "sandbox" //运行环境 sandbox(沙盒) / live(线上)
				// };
				// uni.getProvider({
				// 	service: 'payment',
				// 	success: (res) => {
				// 		if (~res.provider.indexOf('paypal') ) {
				// 			uni.requestPayment({
				// 				provider: "paypal",
				// 				orderInfo,
				// 				success: (response) => {
				// 					resolve(data)
				// 					console.log('success:' + JSON.parse(response.rawdata));
				// 				},
				// 				fail: (err) => {
				// 					reject(err)
				// 					console.log('fail:' + JSON.stringify(err));
				// 				}
				// 			});
				// 		} else {
				// 			uni.navigateTo({
				// 				url: `/pages/other/paypal/paypal`,
				// 				events: {
				// 					change: e => {
				// 						if (e) {
				// 							resolve(data)
				// 						} else {
				// 							reject(e)
				// 						}
				// 					}
				// 				},
				// 				success: res => {
				// 					res.eventChannel.emit('paypal-page', orderNumber)
				// 				}
				// 			})
				// 		}
				// 	},
				// 	fail: e => {
				// 		reject(e)
				// 		console.log('调起支付失败', e)
				// 	}
				// });
			// }
		// }).catch((e) => reject(e))
	})
}
